package com.ftg.learn.day07;

import java.util.Arrays;

/**
 * 十字法求値
 *
 * @author kangjx
 * @version 1.0
 * @date 2023/7/20 下午1:09
 */
public class Array03 {

    /**
     * 求十相临
     * 通过给定的i 与 j求当前元素 相临元素
     */
    public static int[] tenWord(int[][] arr, int i, int j) {
        int[] reArr;
        //第一行  头  和 尾
        if (i == 0 && (j == 0 || j == arr.length - 1)) {
            reArr = new int[2];
            reArr[0] = arr[i][j == 0 ?j+1 : j-1];
            reArr[1] = arr[i + 1][j];
            //最后一行的 头和尾
        } else if (i == arr.length - 1 && (j == 0 || j == arr.length - 1)) {
            reArr = new int[2];
            reArr[0] = arr[i][j == 0 ? j + 1 : j - 1];
            reArr[1] = arr[i - 1][j];
            // 最外侧一圈 上下 t
        } else if (i == 0 || i == arr.length - 1) {
            reArr = new int[3];
            reArr[0] = arr[i][j - 1];
            reArr[1] = arr[i][j + 1];
            reArr[2] = arr[i == 0 ? i + 1 : i - 1][j];
            // 最外侧一圈 左右 t
        } else if (j == 0 || j == arr.length - 1) {
            reArr = new int[3];
            reArr[0] = arr[i - 1][j];
            reArr[1] = arr[i + 1][j];
            reArr[2] = arr[i][j == 0 ? j + 1 : j - 1];
            //十字
        } else {
            reArr = new int[4];
            reArr[0] = arr[i - 1][j];
            reArr[1] = arr[i][j + 1];
            reArr[2] = arr[i + 1][j];
            reArr[3] = arr[i][j - 1];
        }
        return reArr;
    }

    public static int tenWordSum(int[][] arr, int i, int j) {
        //第一行  头  和 尾
        if (i == 0 && (j == 0 || j == arr.length - 1)) {
            return arr[i][j == 0 ? 1 : arr.length - 2] + arr[i + 1][j == 0 ? 0 : arr.length - 1] + arr[i][j];
            //最后一行的 头和尾
        } else if (i == arr.length - 1 && (j == 0 || j == arr.length - 1)) {
            return arr[i][j == 0 ? j + 1 : j - 1] + arr[i - 1][j == 0 ? 0 : arr.length - 1] + arr[i][j];
        } else if (i == 0 || i == arr.length - 1) {
            return arr[i][j - 1] + arr[i][j + 1] + arr[i == 0 ? i + 1 : i - 1][j] + arr[i][j];
            // 最外侧一圈 左右 t
        } else if (j == 0 || j == arr.length - 1) {
            return arr[i - 1][j] + arr[i + 1][j] + arr[i][j == 0 ? j + 1 : j - 1] + arr[i][j];
            //十字
        } else {
            return arr[i - 1][j] + arr[i][j + 1] + arr[i + 1][j] + arr[i][j - 1] + arr[i][j];
        }
    }

    /**
     * @param arr 数组
     * @param i   行数
     * @param j   列数
     * @return 和
     */
    public static int tenSum(int[][] arr, int i, int j) {
        return  ((i - 1) < 0 ? 0 : arr[i - 1][j]) +
                ((j + 1) > arr.length - 1 ? 0 : arr[i][j + 1]) +
                ((i + 1) > arr.length - 1 ? 0 : arr[i + 1][j]) +
                ((j - 1) < 0 ? 0 : arr[i][j - 1]) +
                arr[i][j];
    }

    /**
     * 十字求和转存
     * @param arr 数组
     * @return
     */
    public static int[][] q6(int[][] arr) {
        int[][] arrNew = new int[arr.length][arr.length];
        for (int i = 0; i < arr.length; i++) {
            for (int i1 = 0; i1 < arr.length; i1++) {

                int[] temp = tenWord(arr,i,i1);

                System.out.println(arr[i][i1]+"==>相临"+Arrays.toString(temp));

                arrNew[i][i1] = tenSum(arr, i, i1);
            }
        }
        return arrNew;
    }

    public static void main(String[] args) {
        int[][] as1 = q6(new int[][]{
                {1,  2,  3,  4,   5},
                {6,  7,  8,  9,  10},
                {11, 12, 13, 14, 15},
                {16, 17, 18, 19, 20},
                {21, 22, 23, 24, 25},
        });

        for (int i = 0; i < as1.length; i++) {
            System.out.println(Arrays.toString(as1[i]));
        }

    }

}
